package conceptProofs;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.ccil.cowan.tagsoup.HTMLScanner;
import org.ccil.cowan.tagsoup.Scanner;


public class TestGet {

	/**
	 * Shows the required steps to get the cookie and load it with the server in order to access product information.
	 * This is if you want to use a normal users browsing path.
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String cookieValue = null;
		HttpCookie _parserCookie = null;
		URL _parserUrl;
		HttpURLConnection _parserUrlConnection;
		Collection<String> _parserHtml;
		_parserUrl = new URL("http://fredmeyer.inserts2online.com/storeReview.jsp?divID=701&drpStoreID=00013");
		_parserUrlConnection = (HttpURLConnection) _parserUrl.openConnection();
		_parserUrlConnection.setInstanceFollowRedirects(false);	
		if(_parserUrlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)
		{
				Map<String, List<String>> headers = _parserUrlConnection.getHeaderFields();
				List<String> values = headers.get("Set-Cookie");
				
				System.out.println("getcookie1");
				if( values != null ){
					System.out.println("create cookie");
					
					
					Iterator<String> iter = values.iterator();
					while ( iter.hasNext()) {
						String v = (String) iter.next();
					     if (cookieValue == null)
					     { 
					    	 cookieValue = HttpCookie.parse(v).get(0).toString();
					     	
					     }
					     else
					    	 cookieValue = cookieValue + ";" + HttpCookie.parse(v).get(0).toString();
					     System.out.println("current : " + cookieValue);
					}
					
				}
				_parserUrlConnection.addRequestProperty("cookie", cookieValue);	
		}
		_parserUrlConnection.disconnect();
		_parserUrl = new URL("http://fredmeyer.inserts2online.com/I2O_MainFrame.jsp?pageNumber=1&drpStoreID=13&adId=31984&adPath=KrogerFredMeyer10312010WeeklyAd");
		_parserUrlConnection = (HttpURLConnection) _parserUrl.openConnection();
		_parserUrlConnection.setInstanceFollowRedirects(false);	
		//if(cookieValue != null)
		_parserUrlConnection.addRequestProperty("cookie", cookieValue);
		if(_parserUrlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)
		{
			Map<String, List<String>> headers = _parserUrlConnection.getHeaderFields();
			List<String> values = headers.get("Set-Cookie");
			//cookieValue =null;
			System.out.println("getcookie2");
			if( values != null ){
				System.out.println("create cookie");
				
				
				Iterator<String> iter = values.iterator();
				while ( iter.hasNext()) {
					String v = (String) iter.next();
				     if (cookieValue == null)
				     { 
				    	 cookieValue = HttpCookie.parse(v).get(0).toString();
				     	
				     }
				     else
				    	 cookieValue = cookieValue + ";" + HttpCookie.parse(v).get(0).toString();
				     System.out.println("current : " + cookieValue);
				}
				
			}
			_parserUrlConnection.addRequestProperty("cookie", cookieValue);	

		}
		
		_parserUrl = new URL("http://fredmeyer.inserts2online.com/loadServletXml.jsp?adId=31984&showFlash=false");
		_parserUrlConnection = (HttpURLConnection) _parserUrl.openConnection();
		_parserUrlConnection.setInstanceFollowRedirects(false);	
		_parserUrlConnection.addRequestProperty("cookie", cookieValue);
		if(_parserUrlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)
		{
			Map<String, List<String>> headers = _parserUrlConnection.getHeaderFields();
			List<String> values = headers.get("Set-Cookie");
			//cookieValue =null;
			System.out.println("getcookie3");
			if( values != null ){
				System.out.println("create cookie");
				
				
				Iterator<String> iter = values.iterator();
				while ( iter.hasNext()) {
					String v = (String) iter.next();
				     if (cookieValue == null)
				     { 
				    	 cookieValue = HttpCookie.parse(v).get(0).toString();
				     	
				     }
				     else
				    	 cookieValue = cookieValue + ";" + HttpCookie.parse(v).get(0).toString();
				     System.out.println("current : " + cookieValue);
				}
				
			}
			_parserUrlConnection.addRequestProperty("cookie", cookieValue);	

		}
		_parserUrl = new URL("http://fredmeyer.inserts2online.com/I2O_MainFrame.jsp?pageNumber=1&drpStoreID=13&adId=31984&adPath=KrogerFredMeyer10312010WeeklyAd");
		_parserUrlConnection = (HttpURLConnection) _parserUrl.openConnection();
		_parserUrlConnection.setInstanceFollowRedirects(false);	
		_parserUrlConnection.addRequestProperty("cookie", cookieValue);
		if(_parserUrlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)
		{
			Map<String, List<String>> headers = _parserUrlConnection.getHeaderFields();
			List<String> values = headers.get("Set-Cookie");
			//cookieValue =null;
			System.out.println("getcookie4");
			if( values != null ){
				System.out.println("create cookie");
				
				
				Iterator<String> iter = values.iterator();
				while ( iter.hasNext()) {
					String v = (String) iter.next();
				     if (cookieValue == null)
				     { 
				    	 cookieValue = HttpCookie.parse(v).get(0).toString();
				     	
				     }
				     else
				    	 cookieValue = cookieValue + ";" + HttpCookie.parse(v).get(0).toString();
				     System.out.println("current : " + cookieValue);
				}
				
			}
			_parserUrlConnection.addRequestProperty("cookie", cookieValue);	

		}
		
		_parserUrl = new URL("http://fredmeyer.inserts2online.com/breakout.jsp?referrer=imagemap&imageName=b694969_19.jpg&isBreakoutImage=true&pageNumber=1&breakoutName=b694969_19.jpg&itemId=&showAsPage=true");
		_parserUrlConnection = (HttpURLConnection) _parserUrl.openConnection();
		_parserUrlConnection.setInstanceFollowRedirects(false);	
		_parserUrlConnection.addRequestProperty("cookie", cookieValue);
		if(_parserUrlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)
		{
			Map<String, List<String>> headers = _parserUrlConnection.getHeaderFields();
			List<String> values = headers.get("Set-Cookie");
			//cookieValue =null;
			System.out.println("getcookie5");
			if( values != null ){
				System.out.println("create cookie");
				
				
				Iterator<String> iter = values.iterator();
				while ( iter.hasNext()) {
					String v = (String) iter.next();
				     if (cookieValue == null)
				     { 
				    	 cookieValue = HttpCookie.parse(v).get(0).toString();
				     	
				     }
				     else
				    	 cookieValue = cookieValue + ";" + HttpCookie.parse(v).get(0).toString();
				     System.out.println("current : " + cookieValue);
				}
				
			}
			_parserUrlConnection.addRequestProperty("cookie", cookieValue);	

		}
	}

}
